package com.lmj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmj.dto.DataRequest;
import com.lmj.dto.DataResponse;
import com.lmj.mapper.CurDictionaryMapper;
import com.lmj.mapper.UserMapper;
import com.lmj.pojo.CurDictionary;
import com.lmj.pojo.Handpose;
import com.lmj.pojo.User;
import com.lmj.service.CurDictionaryService;
import com.lmj.utils.RequestBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CurDictionaryServiceImpl extends ServiceImpl<CurDictionaryMapper, CurDictionary> implements CurDictionaryService {

    @Resource
    private CurDictionaryMapper curDictionaryMapper;

    // 响应体
    private DataResponse dataResponse;
    // 响应数据
    private Map<String, String> map = new HashMap<>();

    // 设置当前学习词典的学习位置为1，把之前学习的词典的学习位置为0
    @Override
    public DataResponse setCurDictionary(String request) {
        DataRequest requestBody = RequestBody.getRequestBody(request);

        String userPhone = requestBody.getString("userPhone");
        Long curDictionaryId = requestBody.getLong("curDictionaryId");

        // 获取该用户的其余所有词典
        LambdaQueryWrapper<CurDictionary> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(CurDictionary::getUserPhone, userPhone);
        queryWrapper1.ne(CurDictionary::getCurDictionaryId, curDictionaryId);
        Long cnt = curDictionaryMapper.selectCount(queryWrapper1);

        if (cnt > 0) {
            List<CurDictionary> list = curDictionaryMapper.selectList(queryWrapper1);
            for (CurDictionary cur : list) {
                Long curId = cur.getCurDictionaryId();
                LambdaQueryWrapper<CurDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(CurDictionary::getUserPhone, userPhone);
                queryWrapper2.eq(CurDictionary::getCurDictionaryId, curId);
                CurDictionary curDic = curDictionaryMapper.selectOne(queryWrapper2);
                curDic.setIsStudying(false);
                curDictionaryMapper.update(curDic, queryWrapper2);
            }
        }

        // 查询当前词典-用户关系是否已经创建
        LambdaQueryWrapper<CurDictionary> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(CurDictionary::getUserPhone, userPhone);
        queryWrapper3.eq(CurDictionary::getCurDictionaryId, curDictionaryId);
        Boolean exist = curDictionaryMapper.exists(queryWrapper3);

        if (!exist) {
            // 如果不存在该关系，新建一条关系，并置学习位为1
            CurDictionary curDictionary_new = new CurDictionary();
            curDictionary_new.setCurDictionaryId(curDictionaryId);
            curDictionary_new.setUserPhone(userPhone);
            curDictionary_new.setIsStudying(true);
            curDictionary_new.setCurStudiedWords(0);
            curDictionary_new.setAccuracy(BigDecimal.ZERO);
            curDictionaryMapper.insert(curDictionary_new);
        } else {
            // 如果已存在该关系，查找这条关系，并置学习位为1
            CurDictionary curDictionary_o = curDictionaryMapper.selectOne(queryWrapper3);
            curDictionary_o.setIsStudying(true);
            curDictionaryMapper.update(curDictionary_o, queryWrapper3);
        }

        map.clear();

        // 返回现在的当前词典id
        map.put("curDictionaryId", curDictionaryId.toString());

        return new DataResponse("200", map);
    }

    @Override
    public DataResponse getCurDictionary(String request) {
        DataRequest requestBody = RequestBody.getRequestBody(request);
        String userPhone = requestBody.getString("userPhone");

        // 获取该用户当前正在学习的词典
        LambdaQueryWrapper<CurDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CurDictionary::getUserPhone, userPhone);
        queryWrapper.eq(CurDictionary::getIsStudying, true);
        CurDictionary curDictionary = curDictionaryMapper.selectOne(queryWrapper);

        map.clear();

        map.put("curDictionaryId", curDictionary.getCurDictionaryId().toString());
        map.put("curStudiedWords", curDictionary.getCurStudiedWords().toString());

        return new DataResponse("200", map);
    }

    @Override
    public DataResponse updateCurStudiedWords(String request) {
        DataRequest requestBody = RequestBody.getRequestBody(request);
        String curDictionaryId = requestBody.getString("curDictionaryId");
        String userPhone = requestBody.getString("userPhone");
        String curStudiedWords = requestBody.getString("curStudiedWords");

        // 更新该用户当前正在学习的词典
        LambdaQueryWrapper<CurDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CurDictionary::getCurDictionaryId, curDictionaryId);
        queryWrapper.eq(CurDictionary::getUserPhone, userPhone);
        CurDictionary curDictionary = curDictionaryMapper.selectOne(queryWrapper);

        curDictionary.setCurStudiedWords(Integer.parseInt(curStudiedWords));
        curDictionaryMapper.update(curDictionary, queryWrapper);

        map.clear();

        map.put("curStudiedWords", curStudiedWords);

        return new DataResponse("200", map);
    }

    @Override
    public DataResponse getTotalWords(String request) {
        DataRequest requestBody = RequestBody.getRequestBody(request);
        String userPhone = requestBody.getString("userPhone");

        LambdaQueryWrapper<CurDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CurDictionary::getUserPhone, userPhone);
        List<CurDictionary> list = curDictionaryMapper.selectList(queryWrapper);

        Integer totalWords_i = 0;

        for (CurDictionary dic : list) {
            Long curId = dic.getCurDictionaryId();
            LambdaQueryWrapper<CurDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(CurDictionary::getUserPhone, userPhone);
            queryWrapper2.eq(CurDictionary::getCurDictionaryId, curId);
            CurDictionary curDic = curDictionaryMapper.selectOne(queryWrapper2);
            totalWords_i += (curDic.getCurStudiedWords());
        }

        String totalWords = String.valueOf(totalWords_i);

        map.clear();

        map.put("totalWords", totalWords);

        return new DataResponse("200", map);
    }

}
